package ma.katrav.ws.endpoint;

import ma.katrav.oxm.model.CodeRetour;
import ma.katrav.oxm.model.ResponseDetail;
import ma.katrav.oxm.model.User;
import ma.katrav.ws.oxm.model.authentification.AuthentificationRequest;
import ma.katrav.ws.oxm.model.authentification.AuthentificationResponse;
import ma.katrav.ws.service.AuthentificationService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;


/**
 * Handles subscription requests. 
 * <p>
 *<pre>
 * @Endpoint: similar to Spring MVC's @Controller. 
 * @PayloadRoot: similar to Spring MVC's @RequestMapping</pre>
 */ 
@Endpoint
public class AuthentificationEndpoint {

	protected static Logger logger = Logger.getLogger("endpoint");

	@Autowired @Qualifier("authentificationService")
	private AuthentificationService authentificationService;
	
	// The namespace of both request and response as declared in the XSD file
	public static final String NAMESPACE_URI = "http://katrav.ma/ws/schema/oss";

	// The local name of the expected request.
	public static final String REQUEST_AUTHENTICAT_NAME = "AuthentificationRequest";

	/**
	 * Processes subscriptions. Actual processing is delegated to a service
	 * <p>
	 * In order for this method to be triggered, the localPart and namespace must
	 * match from the incoming XML request. This is exactly similar to Spring MVC.
	 * <p>
	 * In Spring MVC, we declare:
	 * <pre>@RequestMapping(value = "/admin", method = RequestMethod.GET)</pre>
	 * <p>
	 * In Spring WS, we declare:
	 * <pre>@PayloadRoot(localPart = "myRequest", namespace = "http://my.domain.com...")</pre>
	 */
	@PayloadRoot(localPart = REQUEST_AUTHENTICAT_NAME, namespace = NAMESPACE_URI)
	@ResponsePayload
	public AuthentificationResponse processAuthentification( @RequestPayload AuthentificationRequest authneticatRequest) {
		AuthentificationResponse response = null;
		User user = null;
		try {
			logger.debug("Received Authentification request");
			
			try {
				logger.debug("Delegate to service");

				 user = authentificationService.authenticat(authneticatRequest.getLogin(),authneticatRequest.getPassword());
				
			}  catch (Exception e) {
				logger.error("Unable to subscribe");

				// Return response
				response = new AuthentificationResponse();
				ResponseDetail responseDetail = new ResponseDetail();
				responseDetail.setCodeRetour(CodeRetour.FAILURE.getCodeRetour());
				responseDetail.setDescription("description exception");
				
				response.setResponseDetail(responseDetail);
				return response;
			}

		} catch (Exception e) {
			logger.error("Problem with authnetification request");

			// Return response
			response = new AuthentificationResponse();
			ResponseDetail responseDetail = new ResponseDetail();
			responseDetail.setCodeRetour(CodeRetour.FAILURE.getCodeRetour());
			responseDetail.setDescription("description exception");
			
			response.setResponseDetail(responseDetail);
			return response;
		}
		logger.debug("Success in subscribing");
		// Return response
		response = new AuthentificationResponse();
		ResponseDetail responseDetail = new ResponseDetail();
		responseDetail.setCodeRetour(CodeRetour.SUCCES.getCodeRetour());
		responseDetail.setDescription("Response With Secces");
		
		response.setResponseDetail(responseDetail);
		response.setUser(user);
		return response;
	}
}
